package com.twetec.cms.service.file.impl;

import cn.hutool.core.io.FileUtil;
import com.twetec.cms.common.util.fastdfs.FastDFSFile;
import com.twetec.cms.common.util.fastdfs.FastDFSUtil;
import com.twetec.cms.service.file.FastdfsFileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class FastdfsFileServiceImpl implements FastdfsFileService {

    /**
     * fastDFS文件服务器上传文件地址
     */
    @Value("${twetec.fastdfs.server.upload.address}:${twetec.fastdfs.server.upload.port}")
    private String fastDFSServerUploadDomain;

    /**
     * fastDFS文件服务器下载文件地址
     * @param file
     * @return
     */
    @Value("${twetec.fastdfs.server.download.address}")
    private String fastDFSServerDownloadDomain;

    @Override
    public String uploadSiteLogo(MultipartFile file) {

        // 获取文件基本信息
        String fileOriginName = file.getOriginalFilename();
        String fileExt = FileUtil.getSuffix(fileOriginName);

        try {
            FastDFSFile fastDFSFile = new FastDFSFile(file.getBytes(), file.getName(), fileExt, "EDU_ONLINE");
            String fileId = FastDFSUtil.upload(fastDFSFile, fastDFSServerUploadDomain);

            if(null == fileId){
                return null;
            }
            return fastDFSServerDownloadDomain + "/" + fileId;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public String uploadArticleCover(MultipartFile file) {

        // 获取文件基本信息
        String fileOriginalName = file.getOriginalFilename();
        String fileExt = FileUtil.getSuffix(fileOriginalName);

        try {
            // 字节数组，文件名，后缀，
            FastDFSFile fastDFSFile = new FastDFSFile(file.getBytes(), file.getName(), fileExt, "EDU_ONLINE");
            String fileId = FastDFSUtil.upload(fastDFSFile, fastDFSServerUploadDomain);
            if (null == fileId) return null;
            return fastDFSServerDownloadDomain + "/" + fileId;
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public String uploadAttachment(MultipartFile file) {
        // 获取文件基本信息
        String fileOriginalName = file.getOriginalFilename();
        String fileExt = FileUtil.getSuffix(fileOriginalName);

        try {
            // 字节数组，文件名，后缀，
            FastDFSFile fastDFSFile = new FastDFSFile(file.getBytes(), file.getName(), fileExt, "EDU_ONLINE");
            String fileId = FastDFSUtil.upload(fastDFSFile, fastDFSServerUploadDomain);
            if (null == fileId) return null;
            return fastDFSServerDownloadDomain + "/" + fileId;
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public String uploadZip(File file) {
        byte[] bytes = FileUtil.readBytes(file);
        // 字节数组，文件名，后缀，
        FastDFSFile fastDFSFile = new FastDFSFile(bytes, file.getName(), "zip", "EDU_ONLINE");
        String fileId = FastDFSUtil.upload(fastDFSFile, fastDFSServerUploadDomain);
        if (null == fileId) return null;
        return fastDFSServerDownloadDomain + "/" + fileId;
    }

    @Override
    public String uploadImage(MultipartFile file) {
        // 获取文件基本信息
        String fileOriginalName = file.getOriginalFilename();
        String fileExt = FileUtil.getSuffix(fileOriginalName);

        try {
            // 字节数组，文件名，后缀，
            FastDFSFile fastDFSFile = new FastDFSFile(file.getBytes(), file.getName(), fileExt, "EDU_ONLINE");
            String fileId = FastDFSUtil.upload(fastDFSFile, fastDFSServerUploadDomain);
            if (null == fileId) return null;
            return fastDFSServerDownloadDomain + "/" + fileId;
        } catch (IOException e) {
            return null;
        }
    }
}
